package com.dongdongshop.service.impl;

import com.dongdongshop.admin.model.TbItem;
import com.dongdongshop.admin.vo.ItemVO;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.service.ItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private TbItemMapper itemMapper;

    @Override
    public ItemVO getItemById(Long itemId) {
        TbItem item = itemMapper.selectByPrimaryKey(itemId);
        ItemVO vo = new ItemVO();
        BeanUtils.copyProperties(item, vo);
        return vo;
    }

    @Override
    public void subStockCount(List<ItemVO> itemVOList) {
        for (ItemVO itemVO : itemVOList) {
            TbItem item = itemMapper.selectByPrimaryKey(itemVO.getId());
            //减库存
            item.setStockCount(item.getStockCount()-item.getNum());
            itemMapper.updateByPrimaryKeySelective(item);
        }
    }
}
